package cn.net.withub.dataCollector.web.collector;

import cn.edu.hfut.dmic.webcollector.model.CrawlDatums;
import cn.edu.hfut.dmic.webcollector.model.Page;
import cn.edu.hfut.dmic.webcollector.plugin.berkeley.BreadthCrawler;
import cn.net.withub.dataCollector.common.model.TCollectorConfig;
import cn.net.withub.dataCollector.web.service.CollectorService;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.log4j.Logger;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.select.Elements;
import org.springframework.web.context.WebApplicationContext;

/**
 * 新浪微博
 */
public class Weibo extends BreadthCrawler {

    //@Resource
    private CollectorService collectorService;

    //@Resource
    private WebApplicationContext webAppliction;

    String cookie;

    private TCollectorConfig tCollectorConfig;

    private Logger log = Logger.getLogger(this.getClass());
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    public void CollectorUtils(CollectorService collectorService){
        this.collectorService = collectorService;
    }

    public Weibo(String crawlPath, boolean autoParse, WebApplicationContext wac, TCollectorConfig config) {
        super(crawlPath, autoParse);
        webAppliction = wac;
        if(webAppliction !=null) collectorService = (CollectorService) webAppliction.getBean("collectorService");
        if(config != null) tCollectorConfig = config;
    }
    public Weibo(String crawlPath, boolean autoParse) throws Exception {
        super(crawlPath, autoParse);
        //login("luowenjun0420@163.com", "lwj910420");
        /* 获取新浪微博的cookie，账号密码以明文形式传输，请使用小号 */
        //cookie = WeiboCN.getSinaCookie("luowenjun0420@163.com", "lwj910420");
    }


    /**
     * 使用Jsoup模拟登陆 大体思路如下:
     * 第一次请求登陆页面，获取页面信息，包含表单信息，和cookie（这个很重要），拿不到，会模拟登陆不上
     * 第二次登陆，设置用户名，密码，把第一次的cooking，放进去，即可
     * 怎么确定是否登陆成功？登陆后，打印页面，会看到账户的详细信息。
     * @param userName 用户名
     * @param pwd 密码
     * @throws Exception
     */
    public void login(String userName, String pwd) throws Exception {

        Connection con = Jsoup.connect("https://weibo.com/");
        //配置模拟浏览器
        con.header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0");
        //获取响应
        Connection.Response response = con.execute();
        //转换 为dom树
        org.jsoup.nodes.Document d1 = Jsoup.parse(response.body());
        //获取form表单，可以通过查看页面源码代码得知
        Elements et = d1.select("#pl_unlogin_home_login");
        log.info("--------" + et.toString());
        // 获取，cooking和表单属性，下面map存放post时的数据

    }

    public void visit(Page page, CrawlDatums crawlDatums) {
        String url = page.url();
        String html = page.html();
        String contentType = page.contentType();
        String nextUrl = "";
        String title = "", fbsj = "", djcs = "", xxly = "";//信息来源
        Date date = new Date();
        int compare = 0;
        log.info("--------------------------URL:" + url);

    }
    String getStr(Object obj){
        return obj == null || "无".equals(obj.toString()) ? "" : obj.toString();
    }

    public static void main(String[] args) {
        try {
            Weibo crawler = new Weibo("crawl", false);
            //crawler.addSeed("https://weibo.com/");
            //crawler.addSeed("https://passport.weibo.cn/signin/login");
            crawler.addSeed("https://www.baidu.com");
            crawler.setThreads(1);
            crawler.setTopN(500);
            //crawler.setResumable(true);
            crawler.start(500);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
